home *** CD-ROM | disk | FTP | other *** search
- import java.applet.Applet;
- import java.awt.Component;
- import java.awt.Event;
- import java.awt.Graphics;
- import java.util.Stack;
-
- public class CLSFractal extends Applet implements Runnable {
- ContextLSystem cls;
- int fractLevel = 1;
- int repaintDelay = 50;
- boolean incrementalUpdates;
- float startAngle;
- float rotAngle;
- float Xmin;
- float Xmax;
- float Ymin;
- float Ymax;
- int border;
- boolean normalizescaling;
- Thread kicker;
- String savedPath;
-
- public void init() {
- this.cls = new ContextLSystem(this);
- String s = ((Applet)this).getParameter("level");
- if (s != null) {
- this.fractLevel = Integer.parseInt(s);
- }
-
- s = ((Applet)this).getParameter("incremental");
- if (s != null) {
- this.incrementalUpdates = s.equals("true");
- }
-
- s = ((Applet)this).getParameter("delay");
- if (s != null) {
- this.repaintDelay = Integer.parseInt(s);
- }
-
- s = ((Applet)this).getParameter("startAngle");
- if (s != null) {
- this.startAngle = Float.valueOf(s);
- }
-
- s = ((Applet)this).getParameter("rotAngle");
- if (s != null) {
- this.rotAngle = Float.valueOf(s);
- }
-
- this.rotAngle = this.rotAngle / 360.0F * 2.0F * (float)Math.PI;
- s = ((Applet)this).getParameter("border");
- if (s != null) {
- this.border = Integer.parseInt(s);
- }
-
- s = ((Applet)this).getParameter("normalizescale");
- if (s != null) {
- this.normalizescaling = s.equals("true");
- }
-
- }
-
- public void run() {
- Thread me = Thread.currentThread();
- boolean needsRepaint = false;
-
- while(this.kicker == me && this.cls.getLevel() < this.fractLevel) {
- this.cls.generate();
- if (this.kicker == me && this.incrementalUpdates) {
- ((Component)this).repaint();
-
- try {
- Thread.sleep((long)this.repaintDelay);
- } catch (InterruptedException var3) {
- }
- } else {
- needsRepaint = true;
- }
- }
-
- if (this.kicker == me) {
- this.kicker = null;
- if (needsRepaint) {
- ((Component)this).repaint();
- }
- }
-
- }
-
- public void start() {
- this.kicker = new Thread(this);
- this.kicker.start();
- }
-
- public void stop() {
- this.kicker = null;
- }
-
- public boolean mouseUp(Event evt, int x, int y) {
- this.cls = new ContextLSystem(this);
- this.savedPath = null;
- this.start();
- return true;
- }
-
- public void paint(Graphics g) {
- String fractalPath = this.cls.getPath();
- if (fractalPath == null) {
- super.paint(g);
- } else {
- if (this.savedPath == null || !this.savedPath.equals(fractalPath)) {
- this.savedPath = fractalPath;
- this.render((Graphics)null, fractalPath);
- }
-
- for(int i = 0; i < this.border; ++i) {
- g.draw3DRect(i, i, ((Component)this).size().width - i * 2, ((Component)this).size().height - i * 2, false);
- }
-
- this.render(g, fractalPath);
- }
- }
-
- void render(Graphics g, String path) {
- Stack turtleStack = new Stack();
- CLSTurtle turtle;
- if (g == null) {
- this.Xmin = 1.0E20F;
- this.Ymin = 1.0E20F;
- this.Xmax = -1.0E20F;
- this.Ymax = -1.0E20F;
- turtle = new CLSTurtle(this.startAngle, 0.0F, 0.0F, 0, 0, 1.0F, 1.0F);
- } else {
- float frwidth = this.Xmax - this.Xmin;
- if (frwidth == 0.0F) {
- frwidth = 1.0F;
- }
-
- float frheight = this.Ymax - this.Ymin;
- if (frheight == 0.0F) {
- frheight = 1.0F;
- }
-
- float xscale = (float)(((Component)this).size().width - this.border * 2 - 1) / frwidth;
- float yscale = (float)(((Component)this).size().height - this.border * 2 - 1) / frheight;
- int xoff = this.border;
- int yoff = this.border;
- if (this.normalizescaling) {
- if (xscale < yscale) {
- yoff = (int)((float)yoff + ((float)(((Component)this).size().height - this.border * 2) - (this.Ymax - this.Ymin) * xscale) / 2.0F);
- yscale = xscale;
- } else if (yscale < xscale) {
- xoff = (int)((float)xoff + ((float)(((Component)this).size().width - this.border * 2) - (this.Xmax - this.Xmin) * yscale) / 2.0F);
- xscale = yscale;
- }
- }
-
- turtle = new CLSTurtle(this.startAngle, -this.Xmin, -this.Ymin, xoff, yoff, xscale, yscale);
- }
-
- for(int pos = 0; pos < path.length(); ++pos) {
- switch (path.charAt(pos)) {
- case ']':
- turtle = (CLSTurtle)turtleStack.pop();
- break;
- case '[':
- turtleStack.push(turtle);
- turtle = new CLSTurtle(turtle);
- break;
- case 'F':
- if (g == null) {
- this.includePt(turtle.X, turtle.Y);
- turtle.jump();
- this.includePt(turtle.X, turtle.Y);
- } else {
- turtle.draw(g);
- }
- break;
- case '-':
- turtle.rotate(-this.rotAngle);
- break;
- case '+':
- turtle.rotate(this.rotAngle);
- break;
- case 'f':
- turtle.jump();
- }
- }
-
- }
-
- void includePt(float x, float y) {
- if (x < this.Xmin) {
- this.Xmin = x;
- }
-
- if (x > this.Xmax) {
- this.Xmax = x;
- }
-
- if (y < this.Ymin) {
- this.Ymin = y;
- }
-
- if (y > this.Ymax) {
- this.Ymax = y;
- }
-
- }
- }
-